package com.dtm.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class ClientThread implements Runnable {

	public static final String SERVER_IP = "10.10.0.1";
	public static final int SERVERPORT = 1990;
	public static final int NON_CONNECT = 0;
	public static final int CONNECTED = 1;
	private static final int READY = 2;
	private static final int UNREADY = 3;

	public static final int REG = 0;
	public static final int UDL = 1;
	public static final int STR = 2;
	public static final int LIST = 3;

	private String name;
	private int status;
	private int id;
	private Socket socket;
	private ArrayList<Client> clients = new ArrayList<Client>();
	private IUpdateList iUpdateList;
	private ConvertPackge convertPackge;
	private Client client;
	private DataInputStream is;
	private DataOutputStream os;

	public void setiUpdateList(IUpdateList iUpdateList) {
		this.iUpdateList = iUpdateList;
	}

	@Override
	public void run() {
		try {
			InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

			socket = new Socket(serverAddr, SERVERPORT);
			is = (DataInputStream) socket.getInputStream();
			os = (DataOutputStream) socket.getOutputStream();
			while (true) {

			}
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	private void addList(Client client) {
		clients.add(client);
		iUpdateList.onUpdateList(clients);
	}
	private void removeList(Client client) {
		clients.remove(client);
		iUpdateList.onUpdateList(clients);
	}
	private void updateStatus(int status) {
		client.setStatus(status);
	}

	private void sendReg() {
		client = new Client("Thanh", 1, NON_CONNECT);
		byte[] bytes = convertPackge.convertToByte(client, REG);
		try {
			os.write(bytes);
			os.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
}
